<?php

$i__API_key         = isset($_GET['apikey']) ? utf8_decode(trim($_GET['apikey'])) : '';              // API key
$i__plan_id         = isset($_GET['planid']) ? utf8_decode(trim($_GET['planid'])) : 0;               // plan id
$i__title           = isset($_GET['title'])  ? utf8_decode(trim($_GET['title']))  : 'Default title'; // title
$i__use_GoogleChart = (isset($_GET['gchart']) and $_GET['gchart']=='1') ? true : false;

if($i__API_key=='' or $i__plan_id==0)
{
	?>
	<head>
		<title>TLChart</title>
	</head>
	<body>
		<h1>TLChart - Usage</h1>
		<p>
			Here is the API description :
			<ul>
				<li><b>apikey</b> : indicates the API Key to use to generate the chart.</li>
				<li><b>planid</b> : indicates the Plan ID to observe.</li>
				<li><b>gchart</b> : if set to "1", TLChart will use the Google Chart API. by default = 0.</li>
			</ul>
		</p>
		<p>Example : <pre>http://<url>/TLChart/?planid=4705&apikey=khjlkj647sqdsqdu61cd364kjjiuiaa4</pre></p>
		<p><small>
			Created by <a href='mailto:faure.thomas@gmail.com'>Thomas Faur&eacute;</a>
		</small></p>
	</body>
	<?php
}
else
{


define("THIRD_PARTY_CODE","/third_party");
define("PCHART_DIR",dirname(__FILE__) . THIRD_PARTY_CODE . '/pChart2.1.0');
define("SERVER_URL","http://testlink-01.srv.sec.safran/tl-pgm/lib/api/xmlrpc.php");
require_once dirname(__FILE__) . THIRD_PARTY_CODE . '/xml-rpc/class-IXR.php';
require_once dirname(__FILE__) . THIRD_PARTY_CODE . '/dBug/dBug.php';


/* pChart library inclusions */ 
 include(PCHART_DIR . "/class/pData.class.php"); 
 include(PCHART_DIR . "/class/pDraw.class.php"); 
 include(PCHART_DIR . "/class/pPie.class.php"); 
 include(PCHART_DIR . "/class/pImage.class.php"); 


define("DEV_KEY", $i__API_key);

$method='getTotalsForTestPlan';

$args=array();
$args["devKey"]=DEV_KEY;
$args["testplanid"]=$i__plan_id;

$debug=false;

$client = new IXR_Client(SERVER_URL);
$client->debug=$debug;

if(!$client->query("tl.{$method}", $args))
{
	echo "something went wrong - " . $client->getErrorCode() . " - " . $client->getErrorMessage();
	$response=null;
}
else
{
	$response=$client->getResponse();
	$l__percentok = (isset($response[0]['total_tc']) and $response[0]['total_tc']>0) ? number_format(100*$response[0]['details']['passed']['qty']/$response[0]['total_tc'], 2, ',', ' ') : '0';

if($i__use_GoogleChart)
{
	// chart generated with Google Chart 
?>
	<style>
	<!--
	html{
		margin:0px;
		padding:0px;
	}
	-->
	</style>
	<img src="https://chart.googleapis.com/chart?cht=p&
	chd=t:<?php 
		echo "{$response[0]['details']['not_run']['qty']},";
		echo "{$response[0]['details']['passed']['qty']},";
		echo "{$response[0]['details']['failed']['qty']},";
		echo "{$response[0]['details']['blocked']['qty']}";
	?>&
	chs=320x170&
	chl=<?php
		echo "not run ({$response[0]['details']['not_run']['qty']})|";
		echo "passed ({$response[0]['details']['passed']['qty']})|";
		echo "failed ({$response[0]['details']['failed']['qty']})|";
		echo "blocked ({$response[0]['details']['blocked']['qty']})";
		?>&
	chco=000000|8EDF3D|D10D0D|4180BD&
	chtt=<?php echo $i__title;?>|<?php echo "$l__percentok % passed."; ?>
	" />
<?php }
else
{
	// chart generated with pChart 
	// Dataset definition   
	$MyData = new pData();  
	$MyData->addPoints(
		array(
			$response[0]['details']['not_run']['qty'],
			$response[0]['details']['passed']['qty'],
			$response[0]['details']['failed']['qty'],
			$response[0]['details']['blocked']['qty']
		),"Serie1"
		);   
	$MyData->setSerieDescription("ScoreA","Application A"); 
	$MyData->addPoints(array(
		"not run ({$response[0]['details']['not_run']['qty']})",
		"passed ({$response[0]['details']['passed']['qty']})",
		"failed ({$response[0]['details']['failed']['qty']})",
		"blocked ({$response[0]['details']['blocked']['qty']})"
		),"Labels");  
	$MyData->setAbscissa("Labels"); 
	
	$myPicture = new pImage(230,180,$MyData); 
	/* Write the picture title */  
	$myPicture->setFontProperties(array("FontName"=>PCHART_DIR . "/fonts/MankSans.ttf","FontSize"=>11));
	 $myPicture->drawText(10,13,"$l__percentok % passed.",array("R"=>000,"G"=>0,"B"=>0)); 
	 $myPicture->setFontProperties(array("FontName"=>PCHART_DIR . "/fonts/Silkscreen.ttf","FontSize"=>6)); 
	/* Create the pPie object */  
	$PieChart = new pPie($myPicture,$MyData); 
	
	/* Define the slice color */ 
	$PieChart->setSliceColor(0,array("R"=>26,"G"=>26,"B"=>26));   // black 
	$PieChart->setSliceColor(1,array("R"=>142,"G"=>223,"B"=>61)); // green 
	$PieChart->setSliceColor(2,array("R"=>209,"G"=>13,"B"=>13));  // red
	$PieChart->setSliceColor(3,array("R"=>76,"G"=>138,"B"=>195)); // blue
	
	/* Draw an AA pie chart */  
	$PieChart->draw2DPie(110,100,array(
		"Radius" => 50,
		"DrawLabels"=>TRUE,
		"Border"=>TRUE,
		"SecondPass "=>TRUE,
		"BorderR" => 220,
		"BorderG" => 220,
		"BorderB" => 220 
		)); 
	
	$myPicture->autoOutput("pictures/example.draw2DPie.png"); 
}
}
} ?>